/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.DriveSchoolAdapter;
import com.gmail.blackdog1987.carcare.entity.TDriveschool;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.DriveSchoolRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.DriveSchoolResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.DriveSchoolResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class DriveSchoolActivity extends BaseActivity implements OnResponseListener, OnClickListener, OnLastItemVisibleListener, OnRefreshListener2<ListView>,
		OnItemClickListener {
	private Context mContext;
	private PullToRefreshListView mListView;
	private DriveSchoolAdapter mAdapter;

	private DriveSchoolRequestBuilder mReq = new DriveSchoolRequestBuilder();
	private DriveSchoolResponseHandler mHandler = new DriveSchoolResponseHandler();

	private DataPage<TDriveschool> page;
	private String order = "1";
	Intent i;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_driveschool);
		mContext = this;
		i = new Intent(DriveSchoolActivity.this, OrderDriveSchoolActivity.class);
		initViews();
		reg();
		initData(savedInstanceState);
	}

	public void initViews() {
		mListView = (PullToRefreshListView) findViewById(R.id.driveschool_list);
		mListView.setMode(Mode.BOTH);
	}

	public void reg() {
		mListView.setOnRefreshListener(this);
		mListView.setOnLastItemVisibleListener(this);
		mHandler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mListView.setOnItemClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		mAdapter = new DriveSchoolAdapter(mContext, null);
		mListView.setAdapter(mAdapter);
		mReq = new DriveSchoolRequestBuilder();
		mReq.setLat(0.0).setLon(0.0).setPageIndex(1).setOrder(order).setPageSize(20).postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	public void onResponse(IResponseHandler classz) {
		// insert db
		page = ((DriveSchoolResponse) classz.getResponse()).datas;
		if (page.currentPageNo <= 1) {
			mAdapter.setDatas(page.datas);
		} else {
			mAdapter.addDatas(page.datas);
			mAdapter.notifyDataSetChanged();
		}
		mListView.onRefreshComplete();
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "加载失败", Toast.LENGTH_LONG).show();
		mListView.onRefreshComplete();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		mReq.setLat(0.0).setLon(0.0).setPageIndex(1).setOrder(order).setPageSize(20).postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		if (page == null) {
			mReq.setLat(0.0).setOrder(order).setLon(0.0).setPageIndex(1).setPageSize(20).postJsonRequest(mContext, mHandler);
		} else if (page.currentPageNo >= page.lastPageNo) {
			Toast.makeText(mContext, "没有了", Toast.LENGTH_LONG).show();
			mListView.onRefreshComplete();
		} else {
			mReq.setLat(0.0).setOrder(order).setLon(0.0).setPageIndex(page.nextPage).setPageSize(page.pageSize).postJsonRequest(mContext, mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		i.putExtra("data", mAdapter.getItem(position - 1));
		startActivity(i);
	}
}
